<?php
set_time_limit(0);
error_reporting(15);
require_once("header.php");
require_once("functions.php");

$subdomain = @$_REQUEST['tld'];
$keywords = @$_REQUEST['keywords'];

if (empty($subdomain) ) $subdomain = array();

?>
<form action="index.php" method="post" name="keyword_suggestions">
<table width="98%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100" align="left" valign="top" class="bodytext"><strong>Keyterm:</strong></td>
    <td><input type="text" name="keywords" value="<?php echo $keywords; ?>" size="50" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="left" valign="top" class="bodytext"><strong>Domain Extensions:</strong></td>
    <td><table cellspacing="0" cellpadding="0">
        <tr class="bodytext">
          <td><input type="checkbox" value="com" name="tld[]" <?php if(in_array('com', $subdomain)) echo "Checked"; ?> />
            .com&nbsp; </td>
          <td><input type="checkbox" value="net" name="tld[]" <?php if(in_array('net', $subdomain)) echo "Checked"; ?> />
            .net&nbsp; </td>
          <td><input type="checkbox" value="org" name="tld[]" <?php if(in_array('org', $subdomain)) echo "Checked"; ?> />
            .org&nbsp; </td>
          <td><input type="checkbox" value="info" name="tld[]" <?php if(in_array('info', $subdomain)) echo "Checked"; ?> />
            .info&nbsp; </td>
        </tr>
        <tr class="bodytext">
          <td><input type="checkbox" value="biz" name="tld[]" <?php if(in_array('biz', $subdomain)) echo "Checked"; ?> />
            .biz&nbsp; </td>
          <td><input type="checkbox" value="us" name="tld[]" <?php if(in_array('us', $subdomain)) echo "Checked"; ?> />
            .us&nbsp; </td>
          <td><input type="checkbox" value="name" name="tld[]" <?php if(in_array('name', $subdomain)) echo "Checked"; ?> />
            .name&nbsp; </td>
          <td><input type="checkbox" value="in" name="tld[]" <?php if(in_array('in', $subdomain)) echo "Checked"; ?> />
            .in&nbsp;</td>
        </tr>
      </table>
      </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input name="Analyze" type="submit" id="Analyze" value="Analyze" class="button" /></td>
  </tr>
</table>
</form>
<?php
if (isset($_REQUEST['Analyze']) && isset($_REQUEST['keywords']) )
{
	//echo "<pre>";
	
	$htmlcode =  file_get_contents("http://inventory.overture.com/d/searchinventory/suggestion/?mkt=us&lang=en_US&term=".urlencode($keywords));
	
	if ($htmlcode === false ) {
		$error_text = 'Server is busy. Please try again later';
	} else {
	
		$p = strpos($keywords, '<body');
		$text = substr($htmlcode, $p);
		
		$text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
		$text = preg_replace('@<styles[^>]*?>.*?</styles>@si', '', $text);
		$text = preg_replace('@<form[^>]*?>.*?</form>@si', '', $text);
		
		$mytds = get_html_tags($text,'td');
		
		$results = array();
		$j = 0;
		//print_r($mytds);
		$f = 0;
		  
		  if( count($mytds) < 22 )
		  	$f = count($mytds);
		  else
		  	$f = 22;
		
		for($i=0; $i<$f; )
		{
			$m = html_entity_decode(strip_tags($mytds[$i++]['text']));
			$n = strip_tags($mytds[$i++]['text']);

			//$n = preg_replace("/[^a-zA-Z0-9\-_]/", ' ', $n);
	
			$m = trim($m);
			$n = trim($n);
			
			$results[$j][0] = $m;
			$results[$j++][1] = $n;
			
			$results[$j][0] = $m;
			$results[$j++][1] = str_replace(' ','',$n);
		}
		
		if (count($results) == 0 ) {
			$error_text = 'No suggestions for <b>'.$keywords.'</b>';
		}
	}
if (!empty($error_text)) {
	echo '<p class="quotecontent" align="center"><br><br>'.$error_text.'<br><br></p>';
} else {
?>
<table width="98%" border="0" cellspacing="0" cellpadding="0" class="mytable">
  <tr>
    <td height="25"><strong>Results for: <?php echo $keywords; ?></strong> </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="center" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">
	<?php 
	foreach( $subdomain as $ext ) 
	{
	?>
	  <tr>
	  <td height="30" class="headingbar" align="center"><strong>.<?php echo $ext; ?> Extension</strong></td>
	  </tr>
      <tr>
        <td><table width="100%" border="0" cellspacing="1" cellpadding="0">
          <tr class="bodytext" bgcolor="#FDFDFD">
		    <td width="30%" height="25" align="center"><strong>Domain Name</strong></td>
            <td width="28%" height="25" align="center"><strong>Keyword</strong></td>
            <td width="22%" align="center"><strong>Searches / month</strong></td>
			<td width="20%" align="center"><strong>Status</strong></td>
          </tr>
		  <?php
		  $t = 0;
		  
		  for($i=0; $i<count($results); $i++)
		  {
		  ?>
          <tr height="22" class="tr<?php echo (($t % 2 == 0)?'1':'2'); ?>">
		  	<td><?php
			 $keys_domain = html_entity_decode($results[$i][1]);
			 $keys_domain = trim($keys_domain);
			 $keys_domain = preg_replace("/[^a-zA-Z0-9\-_]/", ' ', $keys_domain);	  
		     echo $d = get_domain_name($keys_domain, $ext); ?></td>
            <td><?php echo $results[$i][1]; ?></td>
            <td style="padding-left:4px;"><?php echo $results[$i][0]; ?></td>
			<td style="padding-left:4px;"><?php echo check_domain_active($d); ?></td>
          </tr>
		  <?php
		  $t++;
		  }
		  ?>
        </table></td>
      </tr>
  <?php
  }
  ?>
    </table></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
<?php
	}
}

require_once("footer.php");
?>